package com.study.main.ui.recording

import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.telephony.TelephonyManager
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import com.module.compose.base.ComposeActivity
import com.module.compose.base.composable.CommonPage
import com.module.compose.base.utils.ToastUtils
import com.module.compose.base.viewModel.OurViewModel


class RecordingActivity : ComposeActivity<OurViewModel>() {
    override fun initData() {
        super.initData()
        registerCallReceiver()
        requestPermission()
    }

    /**
     * 请求权限
     */
    private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            val code = 101
            val permissions = arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.PROCESS_OUTGOING_CALLS,
                Manifest.permission.READ_PHONE_STATE,
            )
            //验证是否许可权限
            for (str in permissions) {
                if (checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    //申请权限
                    requestPermissions(permissions, code)
                }
            }
        }
    }

    /**
     * 注册广播
     */
    private fun registerCallReceiver() {
        val receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent) {
                if (intent.action == "android.intent.action.NEW_OUTGOING_CALL") {
                    //拨打电话
                    ToastUtils.show("开始拨打:${intent.action}")
                    println("测试打印:${intent.action}")
                } else {
                    val state = intent.extras!!.getString(TelephonyManager.EXTRA_STATE)
                    //挂断
                    if (state == "IDLE") {
                        ToastUtils.show("电话挂断:${state}")
                        println("测试打印:${state}")
                    }
                }
            }
        }
        registerReceiver(receiver, IntentFilter("android.intent.action.NEW_OUTGOING_CALL"))
        registerReceiver(receiver, IntentFilter("android.intent.action.PHONE_STATE"))
    }

    @Composable
    override fun ContentView() {
        var phoneNum by remember { mutableStateOf("") }

        CommonPage(
            title = "通话录音",
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            OutlinedTextField(
                value = phoneNum,
                onValueChange = {
                    phoneNum = it
                },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone),
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp),
                label = {
                    Text(text = "电话号码:")
                },
            )
            Spacer(modifier = Modifier.height(height = 20.dp))
            Button(onClick = {
                if (phoneNum.isNotEmpty()) {
                    val intent = Intent(Intent.ACTION_CALL)
                    val data = Uri.parse("tel:$phoneNum")
                    intent.data = data
                    startActivity(intent)
                }
            }) {
                Text("打电话")
            }
        }
    }
}